package com.qls.web.modules.op.controller;

import com.qls.core.constant.Constant;
import com.qls.core.dto.PushHotelBody;
import com.qls.core.entity.OpGroup;
import com.qls.core.entity.OpHtlGroup;
import com.qls.core.utils.R;
import com.qls.rabbitmq.SendMsg;
import com.qls.web.common.utils.PageUtils;
import com.qls.web.modules.op.service.GroupService;
import com.qls.web.modules.op.service.HtlGroupService;
import com.qls.web.modules.op.service.SaveGroupHtlService;
import com.qls.web.modules.vo.GroupHotelIdsVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author yj
 * @email yj@gmail.com
 * @date 2021-08-31 10:42:12
 */
@RestController
@RequestMapping("op/htlgroup")
public class HtlGroupController {
    @Autowired
    private HtlGroupService htlGroupService;

    @Autowired
    private SaveGroupHtlService saveGroupHtlService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private SendMsg sendMsg;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("op:htlgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = htlGroupService.sqlQueryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("op:htlgroup:info")
    public R info(@PathVariable("id") Long id) {
        OpHtlGroup htlGroup = htlGroupService.getById(id);

        return R.ok().put("htlGroup", htlGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("op:htlgroup:save")
    public R save(@RequestBody GroupHotelIdsVO groupEntity) {
        saveGroupHtlService.execute(groupEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("op:htlgroup:update")
    public R update(@RequestBody OpHtlGroup htlGroup) {
        htlGroupService.updateById(htlGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("op:htlgroup:delete")
    public R delete(@RequestBody Long[] ids) {
        List<Long> idsList = Arrays.asList(ids);
        List<OpHtlGroup> list = htlGroupService.listByIds(idsList);
        // 不能删除的ids
        List<Long> bedIds = new ArrayList<>();
        List<Long> okList = new ArrayList<>();
        for (OpHtlGroup opHtlGroup : list) {
            if (opHtlGroup.getIsUp().equals(1)) {
                bedIds.add(opHtlGroup.getId());
            } else {
                okList.add(opHtlGroup.getId());
            }
        }
        if (!okList.isEmpty()) {
            htlGroupService.removeByIds(okList);
        }
        if (!bedIds.isEmpty()) {
            return R.error("请先删除：" + bedIds + "店铺上的酒店");
        }
        return R.ok();
    }

    /**
     * 店铺删除
     */
    @RequestMapping("/deleteStore")
    @RequiresPermissions("op:htlgroup:delete")
    public R deleteStore(@RequestBody Map<String, Object> map) {
        List<Integer> hotelIds = (List<Integer>) map.get("hotelIds");
        Integer groupId = Integer.valueOf(map.get("groupId").toString());
        OpGroup opGroup = groupService.queryStoreIdById(groupId);
        PushHotelBody pushHotelBody = new PushHotelBody();
        for (Integer hotelId : hotelIds) {
            pushHotelBody.setHotelId(hotelId.longValue());
            pushHotelBody.setStoreId(opGroup.getStoreId());
            pushHotelBody.setType(-1);
            sendMsg.execute(Constant.RabbitMq.HOTEL, opGroup.getStoreId(), pushHotelBody);
        }
        return R.ok();
    }

}
